package main

import (
	"antiquity_map/config"
	handler "antiquity_map/internal/handlers"
	middleware "antiquity_map/internal/middlewares"
	repository "antiquity_map/internal/repositories"
	service "antiquity_map/internal/services"
	"log"

	"github.com/spf13/viper"
	// "net/http"
)

func main() {
	// 1. 加载配置
	envPath := "./.env"
	if err := config.LoadConfig(envPath); err != nil {
		log.Fatal(err)
	}

	// 2. 初始化数据库连接
	db, err := repository.InitDB()
	if err != nil {
		log.Fatalf("Failed to connect to database: %v", err)
	}
	// 初始化 Redis 连接
	redisClient, err := repository.InitRedis()
	if err != nil {
		log.Fatalf("Failed to connect to Redis: %v", err)
	}
	// 初始化 JWT 中间件
	jwtMiddleware, err := middleware.NewJWTMiddleware(redisClient)
	// 3. 初始化各层依赖
	// Repository 层
	userRepo := repository.NewUserRepository(db)
	locationRepo := repository.NewLocationRepository(db)
	dataRepo := repository.NewDataRepository(db)
	//...

	// Service 层
	userService := service.NewUserService(userRepo, jwtMiddleware)
	locationService := service.NewLocationService(locationRepo, jwtMiddleware)
	dataService := service.NewDataService(dataRepo)
	//...

	// Handler 层
	h := handler.NewHandler(userService, locationService, dataService, redisClient, jwtMiddleware)
	//完整的应该是这样的：h := handler.NewHandler(userService, locationService, dataService, reviewService)

	// 5. 启动服务器
	// 使用 viper 获取配置
	port := viper.GetString("server.port")
	if err := h.Engine().Run(":" + port); err != nil {
		log.Fatal(err)
	}
}
